rm(list=ls())

##############################################################################
# APPENDIX B4 TABLE 1
##############################################################################

# load packages
#library(texreg)
#library(mice)

# load data 
load("data/ccode_data_controls.RData")
load("data/imputed_data_year.RData")

##############
# RUN MODELS #
##############
ccode_years$log_tech_articles <- log(ccode_years$tech_articles + 1)

ccode_years$treat <- ifelse(ccode_years$candidate == 1 & ccode_years$election == 1, 1, 0)

#model 1 (eurepoc attacks no control)
m1 <- lm(attacks ~ treat + factor(country_spell) +
           internet_usage + ict_exports + log_tech_articles, data = ccode_years)


#model 2 (eurepoc attacks control)
d.long$log_tech_articles <- log(d.long$tech_articles + 1)

imp.candidates <- as.mids(d.long)

fit.A <- with(imp.candidates, lm(attacks ~ treat + factor(country_spell) +
                                   internet_usage + ict_exports + log_tech_articles))
m2 <- pool(fit.A)

################
# CREATE TABLE #
################
appendB4_tab1 <- screenreg(list(m1, m2), custom.coef.map = list("treat" = "candidate", 
                                            "internet_usage" = "internet usage", 
                                            "ict_exports" = "ICT exports", 
                                            "log_tech_articles" = "log tech articles"),
       omit.coef = c("country_spell"), custom.model.names = c("Listwise Deletion", "Multiple Imputation"), 
       custom.gof.rows = list("Country Spell FE" = c("YES", "YES")), digits = 3, stars = c(0.01, 0.05, 0.1))

print(appendB4_tab1)

# script complete message 
print("appendB4_tab1 complete")
